<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>商品列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="../static/TG_logo.jpg" type="image/x-icon" />
    <link rel="stylesheet" href="../static/css/font.css">
    <link rel="stylesheet" href="../static/css/xadmin.css">
    <script src="../static/js/jquery.min.js"></script>
    <script type="text/javascript" src="../static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body class="layui-anim layui-anim-up">
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">商品管理</a>
            <a>
                <cite>商品列表</cite></a>
        </span>
        <a class="layui-btn layui-btn-primary layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
            href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon" style="line-height:38px">ဂ</i></a>
    </div>
    <div class="x-body">
        <div class="layui-row">
            <form class="layui-form layui-col-md12 x-so">
                <div class="layui-input-inline">
                    <select name="contrller">
                        <option value="0">商品分类</option>
                        <option value="1">DNF</option>
                        <option value="2">源神</option>
                        <option value="3">王者荣耀</option>
                        <option value="4">英雄联盟</option>
                    </select>
                </div>
                <input type="text" name="title" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
                <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
            </form>
        </div>
        <xblock>
            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
            <button class="layui-btn" onclick="x_admin_show('添加用户','/commodity-add',600,400)"><i
                    class="layui-icon"></i>添加</button>
            <span class="x-right" style="line-height:40px">共有数据：88 条</span>
        </xblock>
        <table class="layui-table">
            <thead>
                <tr>
                    <th>
                        <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i
                                class="layui-icon">&#xe605;</i></div>
                    </th>
                    <th>ID</th>
                    <th>商品标题</th>
                    <th>商品分类</th>
                    <th>单价</th>
                    <th>上架时间</th>
                    <th>库存</th>
                    <th>卖家姓名</th>
                    <th>卖家联系方式</th>
                    <th></th>
                </tr>
            </thead>
            <tbody id=cmdshuju>

            </tbody>
        </table>
        <div class="page">
            <!-- 分页 -->
            <div id="pagination" class="pagination text-center">

            </div>
        </div>
    </div>

    <script>
         var user = window.localStorage.getItem('TGshop_admin')
        $.ajax({
            type: 'get',
            url: 'http://127.0.0.1:8000/v1/TGadmin/commodity/'+user,
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                // alert(data);
                var html = ""
                // 2 数组遍历,将<tr><td>这样的标签和数据做动态的拼接
                $.each(data, function (index, obj) {
                    html += ` 
                 <tr>
                    <td>
                        <div class="layui-unselect layui-form-checkbox" lay-skin="primary"><i
                                class="layui-icon">&#xe605;</i></div>
                    </td>
                    <td>`+ data.id + `</td>
                    <td>`+ data.title + `</td>
                    <td>`+ data.sort + `</td>
                    <td>`+ data.price + `</td>
                    <td>`+ data.on_shelf_time + `</td>
                    <td>`+ data.quantity + `</td>
                    <td>`+ data.sellername + `</td>
                    <td>`+ data.sellertel + `</td>
                    <td class="td-manage">
                        </a>
                        <a title="编辑" onclick="x_admin_show('编辑','/commodity-edit')" href="javascript:;">
                            <i class="layui-icon">&#xe642;</i>
                        </a>
                        <a title="删除" onclick="commodity_del(this,'要删除的id')" href="javascript:;">
                            <i class="layui-icon">&#xe640;</i>
                    </td>
                </tr>`

                });
                // 3 显示
                $('#cmdshuju').html(html);
            }
        });

        $.ajax({
            type: 'get',
            url: 'http://127.0.0.1:8000/v1/TGadmin/commodity/'+user,
            dataType: 'json',
            contentType: 'application/json',
            success: function (data) {
                var pageNum=1,pageSize = 10,totalRow = data.cmd_num
                initPagination();
                /** 更多-分页初始化 */
                function initPagination() {
                    /** 生成分页 */
                    layui.use('laypage', function () {
                        var laypage = layui.laypage;
                        //执行一个laypage实例
                        laypage.render({
                            elem: 'pagination', // ID，不用加 # 号
                            theme: '#c90b0b',   // 自定义主题
                            curr: parseInt(pageNum), // 当前页
                            count: parseInt(totalRow), // 数据总数，从服务端得到,
                            limit: parseInt(pageSize), // 每页显示的条数。laypage将会借助 count 和 limit 计算出分页数。
                            layout: ['prev', 'page', 'next', 'count', 'skip', 'limit'], // 按钮自定义排版,
                            jump: function (obj, first) { // 切换分页的回调
                                if (!first) {
                                    pageNum = obj.curr;
                                    pageSize = obj.limit;
                                    initPagination();
                                }
                            }
                        });
                    });
                }
            }
        })
        /*商品-删除*/
        function commodity_del(obj, id) {
            layer.confirm('确认要删除吗？', function (index) {
                //发异步删除数据
                var id = $(this.id).val();
                $.ajax({
                    type: 'delete',
                    url: 'http://127.0.0.1:8000/v1/TGadmin/commodity/'+user,
                    data: JSON.stringify(id),
                    dataType: 'json',
                    contentType: 'application/json',
                    success(data) {
                        if (data.code == 200) {
                            $(obj).parents("tr").remove();
                            layer.msg('已删除!', { icon: 1, time: 1000 });
                        } else {
                            alert(data.error)
                        }
                    }
                });
            });
        }

        // var check_value =[]; 
        // // 根据复选框的name值，遍历每一个已选中的名字为该name值的复选框
        // // selectName -- 请改为你自己的name值
        // $('layui-form-checked').each(function(){
        //     //将选中的值添加到数组check_value中
        //     check_value.push($(this).val());   
        //     alert($(this).val());
        // }); 
        function delAll(argument) {
            var data = tableCheck.getData();
            //选中的个数
            var long = data.length;
            var arr = [];
            //给数组赋值选中的id
            for (var i = 0; i < long; i++) {
                //children('.td)为选中class为td的值，这个值为我们要选中的id.
                arr[i] = $(".layui-form-checked").not('.header').parents('tr').children('.td').eq(i).html();
            }
            layer.confirm('确认要删除吗？' + data, function (index) {
                //将选中的数据arr，发异步进行删除
                $.ajax({
                    type: 'delete',
                    url: 'http://127.0.0.1:8000/v1/TGadmin/commodity/'+user,
                    dataType: 'json',
                    contentType: 'application/json',
                    //设置请求头
                    beforeSend:function(request){
					request.setRequestHeader("authorization",localStorage.getItem('TGshop_token'))
					},
                    success(data) {
                        if (data.code == 200) {
                            layer.msg('删除成功', { icon: 1 });
                            $(".layui-form-checked").not('.header').parents('tr').remove();
                        }else {
                            alert(data.error)
                        }
                    }
                });    
            });
        }
    </script>
</body>

</html>